﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Wpf_ClientLeger.BusinessObject;
using Wpf_ClientLeger.ServicePreparation;

namespace Wpf_ClientLeger.IHM
{
    public partial class SaisieBPAnomalie : Window
    {
        SaisieBPQuantite formQte;
        String[] ListAnomalies;
        ItemCleTexte ict;

        public SaisieBPAnomalie(SaisieBPQuantite bpQte)
        {
            InitializeComponent();
            this.formQte = bpQte;

            //garnissage combobox
            ListAnomalies = this.formQte.formBp.ServicePrepa.ChargerAnomalies();

            ict = new ItemCleTexte("", 0);
            this.cb_anomalies.Items.Add(ict);            //un choix vide pour la remise à blanc
            this.cb_anomalies.SelectedItem = ict;
            foreach (String anomalie in ListAnomalies)
            {
                string[] itemAno = anomalie.Split(new char[]{';'});

                ict = new ItemCleTexte(itemAno[1], int.Parse(itemAno[0]));
                this.cb_anomalies.Items.Add(ict);

                if (this.formQte.formBp.ligneBp.idAnomalie == ict.ValeurARetourner)
                {
                    this.cb_anomalies.SelectedItem = ict;
                }
            }

        }

        //appel de la méthode d'écriture de la ligne BP
        private void btn_valider_Click(object sender, RoutedEventArgs e)
        {
            ItemCleTexte currentItem = (ItemCleTexte)this.cb_anomalies.SelectedItem;
            this.formQte.formBp.ligneBp.idAnomalie = currentItem.ValeurARetourner;

            Quit();
        }

        private void btn_retour_Click(object sender, RoutedEventArgs e)
        {
            Quit();   
        }

        private void Quit()
        {
            this.Close();
            this.formQte.textBox_qte.Focus();
            this.formQte.textBox_qte.SelectAll();
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btn_valider_Click(sender, e);
            }
            else if (e.Key == Key.Escape)
            {
                Quit();
            }
        }

    }
}
